--
-- Author: ping.wang
-- Date: 2019-03-01 14:45:27
--
-- PvpDuelPlayerPanel
--

local ClassRef = app.mvc.createPanelClass(...)
local AvatarIconNode = require("app.widget.AvatarIconNode")
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PvpDuelPlayerNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._name = self._ui.name
    self._zlText = self._ui.zl:findChild("text")
    self._lvText = self._ui.Panel_lv:findChild("num")
    self._rankText = self._ui.rank
    self._playerIcon = AvatarIconNode.new(self._ui.Panel, "player")
    self._classIcon = self._ui.class_icon --职业icon
    self._ui.btn_refresh:addClickEventListener(function()
        executeFunc(self._onOperate, "refreshDuelOpponent",self._idx)
    end)

    self._ui.Panel:setTouchEnabled(true)
    self._ui.Panel:addClickEventListener(handler(self, self.selectCurrentOpponent))

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

function ClassRef:selectCurrentOpponent()
    executeFunc(self._onOperate, "selectDuelOpponent", self._idx)
end

--data is PvpDuelMemberVo
function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data
        local udbInfo = data:getUserDataBaseInfo()
        self._playerIcon:update(udbInfo)
        self._name:setString(udbInfo.name)
        self._zlText:setString(tostring(udbInfo.combatPower))
        self._lvText:setString(tostring(udbInfo.lv))
        self._rankText:setString(string.format(L("duelGametips10"), tostring(data:getRankNum())))
        self._classIcon:loadTexture(udbInfo:getClassIcon())
        self._ui.btn_refresh:setVisible(data:canRefresh())
    else
        self:setVisible( false )
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:setSelectedState(bl)
    self._ui.bg_1:setVisible(bl)
    self._ui.bg_2:setVisible(not bl)
    if bl then
        self._ui.Panel:setScale(1)
        self:setLocalZOrder(3)
    else
        self._ui.Panel:setScale(0.97)
        self:setLocalZOrder(1)
    end
end

function ClassRef:setIdx(idx)
    self._idx = idx
end

function ClassRef:getIdx()
    return self._idx
end

return ClassRef
